const Utils = (function () {
  function getStyle(obj, attr) {
    var val
    if (window.getComputedStyle) {
      val = window.getComputedStyle(obj, null)[attr]
    } else {
      return obj.currentStyle[attr]
    }
    return parseInt(val)
  }

  function animate(obj, json, callback) {
    clearInterval(obj.timer)
    obj.timer = setInterval(() => {
      var closeTime = true
      for (var attr in json) {
        let target = 0
        let current = 0
        if (attr == 'opacity') {
          // 遇到opacity，要放大100倍
          target = json[attr] * 100
          current = parseFloat(getStyle(obj, attr)) * 100
        } else {
          target = json[attr]
          current = parseInt(getStyle(obj, attr))
        }

        let step = (target - current) / 10
        step = step > 0 ? Math.ceil(step) : Math.floor(step)

        if (attr == 'opacity') {
          obj.style[attr] = (current + step) / 100
        } else if (attr == 'zIndex') {
          // zIndex不要参与动画
          obj.style[attr] = target
        } else {
          obj.style[attr] = current + step + 'px'
        }

        if (target != current) {
          closeTime = false
        }
      }
      if (closeTime == true) {
        clearInterval(obj.timer)
        if (typeof callback == 'function') {
          callback()
        }
      }
    }, 40)
  }
  return {
    animate
  }
})()
